package com.lmqb.util;

import java.io.File;
import java.io.IOException;
import java.security.MessageDigest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;
import java.util.UUID;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.web.multipart.MultipartFile;

public class StrUtil {
	/**
	 * 
	 * 生成随机字符串
	 * @param length
	 * @return
	 */
	public static String getRandomString(int length)
	{
		Random random = new Random();
		StringBuffer sb = new StringBuffer();

		if (length < 1)
		{
			length = 1;
		}
		String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
		for (int i = 0; i < length; i++)
		{
			int number = random.nextInt(base.length());
			sb.append(base.charAt(number));
		}
		return sb.toString();
	}
	
	
	
	/**
	 * 
	 * 生成验证码。
	 * @param length
	 * @return
	 */
	public static String getRandomNumber(int length)
	{
		Random random = new Random();
		StringBuffer sb = new StringBuffer();

		if (length < 1)
		{
			length = 1;
		}
		for (int i = 0; i < length; i++)
		{
			int number = random.nextInt(10);
			sb.append(number+"");
		}
		return sb.toString();
	}
	
	public static void main(String args[]){
//		boolean a=isMobile("17096177665");
//		System.out.println(a);
	}
	
	/**
	 * 判断是否为空.true：空。false：非空
	 * @param str
	 * @return
	 */
	public static boolean isNull(String str){
		boolean rs=(str==null||"".equals(str)||"null".equals(str));
		return rs;
	}
	
	/**
	 * MD5加密密码
	 * @param str
	 * @return
	 */
	public static String Get_MD5(String str){
		String word=null;
		try {
			MessageDigest md5=MessageDigest.getInstance("MD5");
			md5.update(str.getBytes());
			byte[] data=md5.digest();
			StringBuffer sb=new StringBuffer(data.length*2);
			for(byte b:data){
				if ((b & 0xFF) < 0x10) sb.append("0");
		        sb.append(Integer.toHexString(b & 0xFF));
			}
			word=sb.toString();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			word=null;
		}
		return word;
	}
	
	static public String addFile(MultipartFile fileImg,String path,String path1){
		String originalFileName = fileImg.getOriginalFilename();
		int a=originalFileName.indexOf(".");
		String str=originalFileName.substring(a);
		String fileName=UUID.randomUUID().toString()+str;
		String path2=path+path1;
		File targetFile = new File(path2, fileName);
		if (!targetFile.exists())
		{
			targetFile.mkdirs();
			try {
				fileImg.transferTo(targetFile);
			} catch (IllegalStateException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		String picUrl=path1+"/"+fileName;
		return picUrl;
	}
	
	static public void delFile(String pathOld,String path){
		pathOld=path+pathOld;
		File fileOld = new File(pathOld);
		boolean b=fileOld.exists();
		boolean c=fileOld.isFile();
		System.out.println(b+" "+c);
		if (fileOld.exists()&&fileOld.isFile() )
		{
			fileOld.delete();
			System.out.println("删除单个文件1" + pathOld + "成功！");
		}
		else
		{
			System.out.println("删除单个文件1" + pathOld + "失败！");
		}
	}
	
	static public int timeToInt(Date date){
		if(date==null){
			return 0;
		}
		return (int)(date.getTime()/1000);
	}
	
	static public Date intToTime(Integer time){
		if(time==null){
			time=0;
		}
		return new Date((long)(time)*1000);
	}
	
	static public String intToStr(Integer time){
		if(time==null){
			return "";
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date=new Date(((long)time*1000));
		String timeStr=sdf.format(date);
		return timeStr;
	}
	
	static public String timeToStr(Date date){
		if(date==null){
			return "";
		}
		SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String timeStr=sdf.format(date);
		return timeStr;
	}
	
	//时间字符串转时间
	static public Date strToTime(String time){
		if(isNull(time)){
			return null;
		}
		SimpleDateFormat sdf=null;
		Date date=new Date();
		if(time.length()>11){
			sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				date = sdf.parse(time);
				String str=sdf.format(date);
				System.out.println(str);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}else{
			sdf=new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = sdf.parse(time);
				String str=sdf.format(date);
				System.out.println(str);
			} catch (ParseException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		
		
		return date;
	}
	
	//时间字符串转整数
	static public Integer strToInt(String time){
		Integer timeInt=0;
		if(isNull(time)){
			timeInt=0;
		}
		Date date=strToTime(time);
		timeInt=timeToInt(date);
		return timeInt;
	}
	
	/**
	 * 
	 * 判断是否是手机号
	 * @param phone
	 * @return
	 */
	public static boolean isMobile(String phone)
	{// 判断是否是手机号
		if(isNull(phone)){
			return false;
		}
		
		//String str = "^((13[0-9])|(15[0-9])|(17[0-9])|(14[57])|(18[0-9]))\\d{8}";
		String  str="/^1[3|4|5|7|8][0-9]{9}$/";
		//String str="^((13[0-9])|(15[^4])|(18[0-9])|(17[0-8])|(147,145))\\d{8}$";
		Pattern p = Pattern.compile(str);
		Matcher m = p.matcher(phone);
		boolean rs = m.matches();
		return rs;
	}
	
}
